/**
 * 定义站点地图模块
 */
(function(window, angular, undefined){
	var StationMapModule = angular.module('water.apps.zhuhai.stationMap.stationMapManager', ['water.entity.monitorStation',
	                                                                                         'water.common.dateUtil']);
		
	//污染源站点地图
	StationMapModule.controller('stationBaiduMapManagerController',function($wdp,$q,$scope,$controller,$monitorStationService,$dateUtil){
		
		/**
		 * 客户要求地图加载完就打开站点信息框
		 * 这里用来检查站点数据是否加载完成
		 */
		$scope.stationListLoadedCheckInterval;
		/**
		 * infoWindows上的站点信息刷新器
		 */
		$scope.stationInfoRefeashInterval;
		/**
		 * 当前打开的windows
		 */
		$scope.curInfoWindows;
		
		/**
		 * 查询站点
		 */
		$scope.queryStation=function(){
			var queryObj = $scope._getQueryObject();
			$monitorStationService.getListWithCompany(queryObj).then(function(data){
				$scope.stationList = data.data;
				_renderToMap(data.data);
			});
		};
		
		/**
		 * 获取查询参数
		 */
		$scope._getQueryObject = function(){
			var waterType = getWaterType();
			var queryObj = {
					WATER_TYPE:waterType,
					BUILD_STATUS:'0',
					STATION_NAME:$scope.stationName
			};
			return queryObj;
		}
		
		function _renderToMap(stations){
			
			for(var i=0;i<stations.length;i++){
				var stationItem = stations[i];
				var point = new BMap.Point(stationItem.X,stationItem.Y);
				var imgUrl = "";
				if(stationItem.STATION_STATUS == null || stationItem.STATION_STATUS == ""){
					imgUrl = getJsBasePath()+"/wdp/images/sw/stationStatus/3.png";
				}
				else{
					imgUrl = getJsBasePath()+"/wdp/images/sw/stationStatus/"+stationItem.STATION_STATUS+".png";
				}
				var myIcon = new BMap.Icon(imgUrl, new BMap.Size(15,15), {  
	            });  
				var marker = new BMap.Marker(point,{icon:myIcon});  // 创建标注
				marker.data = stationItem;
				marker.addEventListener("click", function(){ 
					$scope.openStationInfoWindows(this);
				})
				$wdp.bmap.addOverlay(marker);              // 将标注添加到地图中
			}
		}
		
		$scope.openStationInfoWindows = function(marker){
			var sContentDOM = $("#mapInfoWindow .contentPane").clone();
			$("#stationmapclass .list").removeClass("selected");
			$("div.list[code='"+marker.data.CODE+"']").addClass("selected");
			var sContent =$("<div></div>").append($("<div style='font-weight:bold;'>"+marker.data.NAME+"&nbsp<span style='font-weight: 100;color:blue' id='refeashTip'></span></div>").append($("<div id='bmap_window_div'></div>").append(sContentDOM))).html();
			$scope.curInfoWindows = new BMap.InfoWindow(sContent); 	
			
			marker.openInfoWindow($scope.curInfoWindows);
			
			clearInterval($scope.stationInfoRefeashInterval);
			$scope.drawStationData(marker.data.DEVICE_NUM,true);
			/**
			 * 定时刷新数据
			 */
			$scope.stationInfoRefeashInterval = setInterval(function(){
				$("#refeashTip").html("更新中………");
				/**
				 * 延迟执行，让人注意到“更新中……”这个信号
				 * 不然会一闪而过
				 */
				setTimeout(function(){
					$scope.drawStationData(marker.data.DEVICE_NUM,false);
				}, 1000);				
			}, 10000);
		}
		
		/**
		 * 在infowindow上显示站点数据信息
		 * reDraw:是否重绘
		 */
		$scope.drawStationData = function(mn,reDraw){
			var WaterType=getWaterType();
			var endDateStr = $dateUtil.formatDate(new Date(),"yyyy-MM-dd hh:mm:ss");
			var startDate = $dateUtil.addHour(new Date(),-1);
			var startDateStr = $dateUtil.formatDate(startDate,"yyyy-MM-dd hh:mm:ss");
			
			var queryObj = {
					MN:mn,
					PARMS:"065,060,ss_zhuodu,101,111,001,cond,do,phtem",
					START:startDateStr,
					END:endDateStr,
					WATER_TYPE:"1"
			};
			$waterDataCommon.getPageRows("/MonitorData/getLastRealTimeData.do","stationInfo",queryObj,100,1,function(d){
				var parms = d.pollInfo;
				var data = d.data[0];
				var table = $("#bmap_window_div table[tag='realTimeData']");
				table.empty();
				
				if(!data){
					table.append("<tr><td colspan='3'>无数据</td></tr>");
					return;
				}
				var parmDatas = d.parmData;
				
				for(var poll in parms){
					var pollItem = parms[poll];
					if(pollItem.name == "累积流量"){
						continue;
					}
					var tr = "<tr>";
					tr += "<td style='height:22px;line-height:22px'>"+pollItem.name+":</td>";
					tr += "<td style='height:22px;line-height:22px'>"+data['AVG_'+poll.toUpperCase()]+"</td>";
					tr += "<td style='height:22px;line-height:22px'>"+pollItem.unit+"</td>";
					tr += "</tr>";
					table.append(tr);
				}
				$("#refeashTip").html("");
				/**
				 * 如果是刷新，那么就不用重绘
				 * 重绘是为了让bmap重新确定窗口的高和宽
				 * 如果是刷新就不需要了
				 */
				if(reDraw){
					$scope.curInfoWindows.redraw();
				}
			});
		}
		
		//定位
		$scope.onLocateClick = function(data){
			var x = data.X;
			var y = data.Y;
			$wdp.bmap.centerAndZoom(new BMap.Point(x,y), 15);
			var stationItem = data;
			var point = new BMap.Point(x,y);
			var imgUrl = "";
			if(stationItem.STATION_STATUS == null || stationItem.STATION_STATUS == ""){
				imgUrl = getJsBasePath()+"/wdp/images/sw/stationStatus/3.png";
			}
			else{
				imgUrl = getJsBasePath()+"/wdp/images/sw/stationStatus/"+stationItem.STATION_STATUS+".png";
			}
			var myIcon = new BMap.Icon(imgUrl, new BMap.Size(15,15), {});  
			var marker = new BMap.Marker(point,{icon:myIcon});  // 创建标注
			marker.data = stationItem;
			marker.addEventListener("click", function(){ 
				$scope.openStationInfoWindows(this,true);
			})
			$wdp.bmap.addOverlay(marker);  
			$scope.openStationInfoWindows(marker,true);
			
		}
		
		/**
		 * 初始化百度地图
		 */
		function _initBaiduMap(){
			if($wdp.bmap == null){
				$wdp.bmap = new BMap.Map("bmap");    // 创建Map实例
				$wdp.bmap.centerAndZoom(new BMap.Point(113.56257,22.271449), 12);  // 初始化地图,设置中心点坐标和地图级别
				//设置只显示电子地图和卫星混合图
				$wdp.bmap.setCurrentCity("珠海");          // 设置地图显示的城市 此项是必须设置的
				$wdp.bmap.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
			}
			$wdp.bmap.addEventListener("tilesloaded",autoOpenStationWindows);
		}
		
		/**
		 * 判断站点列表是否已加载
		 * 是的话自动打开站点列表的第一个站点的地图窗口
		 */
		function autoOpenStationWindows() {
			
			$scope.stationListLoadedCheckInterval = setInterval(function(){
				if($scope.stationList != null && $scope.stationList.length > 0){
					$scope.onLocateClick($scope.stationList[0]);
					clearInterval($scope.stationListLoadedCheckInterval);
				}
			}, 300)
			
			$wdp.bmap.removeEventListener("tilesloaded",autoOpenStationWindows);
		}
		
		_initBaiduMap();
		$scope.queryStation();
	});
	
}(window, angular));


